package com.workflow.engine.eb.steps.jd.login;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Splitter;
import com.workflow.engine.core.flow.StepState;
import com.workflow.engine.parser.annotation.PGConfig;
import com.workflow.engine.parser.annotation.PGScheme;
import com.workflow.engine.parser.constant.HttpMethod;
import com.workflow.engine.parser.flow.impl.AbstractStep;
import com.workflow.engine.parser.strategy.StrategyType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import static com.workflow.engine.eb.util.BizMethodUtil.createLoginQueryString;
import static com.workflow.engine.eb.util.BizMethodUtil.jsonEncode;
import static com.workflow.engine.parser.constant.HttpMethod.POST;
import static com.workflow.engine.parser.utils.StepStateUtil.successfulStepState;

/**
 * 京东登录
 *
 * Created by Administrator on 2017/6/7.
 */

@PGConfig({
        @PGScheme(requestParamName = "uuid", strategy = StrategyType.CONTEXT, strategyNeedParams = { "uuid", ""}),
        @PGScheme(requestParamName = "seqSid", strategy = StrategyType.CONTEXT, strategyNeedParams = {"seqSid", ""}),
        @PGScheme(requestParamName = "sa_token", strategy = StrategyType.CONTEXT, strategyNeedParams = { "sa_token", ""}),
        @PGScheme(requestParamName = "pubKey", strategy = StrategyType.CONTEXT, strategyNeedParams = { "pubKey", ""}),
        @PGScheme(requestParamName = "nloginpwd", strategy = StrategyType.CONTEXT, strategyNeedParams = {"nloginpwd", ""}),
        @PGScheme(requestParamName = "loginType", strategy = StrategyType.CONTEXT, strategyNeedParams = {"loginType", ""}),
        @PGScheme(requestParamName = "loginname", strategy = StrategyType.CONTEXT, strategyNeedParams = {"loginname", ""}),
        @PGScheme(requestParamName = "fp", strategy = StrategyType.CONTEXT, strategyNeedParams = {"fp", ""}),
        @PGScheme(requestParamName = "eid", strategy = StrategyType.CONTEXT, strategyNeedParams = {"eid", ""}),
        @PGScheme(requestParamName = "chkRememberMe", strategy = StrategyType.CONTEXT, strategyNeedParams = {"chkRememberMe", ""}),
        @PGScheme(requestParamName = "authcode", strategy = StrategyType.CONTEXT, strategyNeedParams = {"authcode", ""}),
        @PGScheme(requestParamName = "_t", strategy = StrategyType.CONTEXT, strategyNeedParams = {"_t", ""}),
})
public class JDLoginService extends AbstractStep {

    private static final Logger logger = LoggerFactory.getLogger(JDLoginService.class);

    @Override
    public Logger getLogger() {
        return logger;
    }

    @Override
    protected String getRequestUrl() {
        return "https://passport.jd.com/uc/loginService";
    }

    @Override
    protected String getQueryString(Map<String, Object> context) {
        return createLoginQueryString((String)context.get("uuid"));
    }

    @Override
    protected HttpMethod getHttpMethod() {
        return POST;
    }

    @Override
    protected Map<String, String> getHeaders() {
        return new HashMap<String, String>(){{
            put("Host", "passport.jd.com");
            put("Connection", "keep-alive");
            put("Accept", "text/plain, */*; q=0.01");
            put("Origin", "https://passport.jd.com");
            put("X-Requested-With", "XMLHttpRequest");
            put("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
            put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            put("Referer", "https://passport.jd.com/new/login.aspx");
            put("Accept-Encoding", "gzip, deflate, br");
            put("Accept-Language", "zh-CN,zh;q=0.8");
        }};
    }

    @Override
    protected StepState handleResponse(Map<String, Object> context, Object response) throws Exception {
        JSONObject jsonObject = JSON.parseObject(jsonEncode(response.toString().replaceAll("\\(|\\)", "")));
        logger.info("result is {}", jsonObject);
        return successfulStepState(this);
    }

    private String getParam(String url, String name) {
        String params = url.substring(url.indexOf("?") + 1, url.length());
        Map<String, String> split = Splitter.on("&").withKeyValueSeparator("=").split(params);
        return split.get(name);
    }
}
